//
//  GJFMerchantTableViewCell.m
//  Wedding
//
//  Created by 郭静芬 on 2017/1/2.
//  Copyright © 2017年 郭静芬. All rights reserved.
//

#import "GJFMerchantTableViewCell.h"

@interface GJFMerchantTableViewCell ()
@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) CAGradientLayer *gradientLayer;
@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UIImageView *imageUser;
@property (nonatomic, strong) UIView *collectBgView;
@property (nonatomic, strong) UIButton *imageCollect;

@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UILabel *tag1;
@property (nonatomic, strong) UILabel *tag2;
@property (nonatomic, strong) UILabel *tag3;
@property (nonatomic, strong) UIView *tag1BgView;
@property (nonatomic, strong) UIView *tag2BgView;
@property (nonatomic, strong) UIView *tag3BgView;
@end

@implementation GJFMerchantTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self imageV];
        [self view];
        [self imageUser];
        [self name];
    }
    return self;
}

- (void)setMerchantModel:(GJFMerchantModel *)merchantModel {
    _merchantModel = merchantModel;
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:merchantModel.banner] placeholderImage:[UIImage imageNamed:@"占位图"]];
    [self.imageUser sd_setImageWithURL:[NSURL URLWithString:merchantModel.avatar] placeholderImage:[UIImage imageNamed:@"占位图"]];
    self.name.text = merchantModel.nickname;
    
    self.tag1BgView.hidden = NO;
    self.tag2BgView.hidden = NO;
    self.tag3BgView.hidden = NO;
    self.tag1.hidden = NO;
    self.tag2.hidden = NO;
    self.tag3.hidden = NO;
    NSArray *tagArr = [merchantModel.taglist componentsSeparatedByString:@","];
    //根据标签前面三个的个数作隐藏
    if (!tagArr || tagArr.count == 0 || [merchantModel.taglist length] == 0) {
        self.tag1.hidden = YES;
        self.tag2.hidden = YES;
        self.tag3.hidden = YES;
        self.tag1BgView.hidden = YES;
        self.tag2BgView.hidden = YES;
        self.tag3BgView.hidden = YES;
        
    } else {
        if (tagArr[0]) {
            self.tag1.text = tagArr[0];
        }
        else {
            self.tag1.hidden = YES;
            self.tag1BgView.hidden = YES;
        }
        if (tagArr.count > 1) {
            self.tag2.text = tagArr[1];
        }
        else {
            self.tag2.hidden = YES;
            self.tag2BgView.hidden = YES;
        }
        if (tagArr.count > 2) {
            self.tag3.text = tagArr[2];
        }
        else {
            self.tag3.hidden = YES;
            self.tag3BgView.hidden = YES;
        }
    }
    [self collectBgView];
    self.imageCollect.selected = [merchantModel.is_collect boolValue];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.imageV.frame = CGRectMake(5 * XX, 5 * YY, WIDTH - 10 * XX, self.contentView.frame.size.height - 5 * YY);
    self.view.frame = self.imageV.frame;
    self.gradientLayer.frame = self.view.bounds;
    self.imageUser.frame = CGRectMake(15 * XX, 120 * YY, 50 * YY, 50 * YY);
    NSArray *tagArr = [_merchantModel.taglist componentsSeparatedByString:@","];
    if (!tagArr || tagArr.count == 0 || [_merchantModel.taglist length] == 0) {
        self.name.frame = CGRectMake(75 * XX, 130 * YY, WIDTH - 100 * XX, 30 * YY);
    } else {
        self.name.frame = CGRectMake(75 * XX, 115 * YY, WIDTH - 100 * XX, 30 * YY);
    }
    self.tag1BgView.frame = CGRectMake(75 * XX, 150 * YY, 70 * XX, 20 * YY);
    self.tag2BgView.frame = CGRectMake(150 * XX, 150 * YY, 70 * XX, 20 * YY);
    self.tag3BgView.frame = CGRectMake(225 * XX, 150 * YY, 70 * XX, 20 * YY);
    self.tag1.frame = CGRectMake(75 * XX, 150 * YY, 70 * XX, 20 * YY);
    self.tag2.frame = CGRectMake(150 * XX, 150 * YY, 70 * XX, 20 * YY);
    self.tag3.frame = CGRectMake(225 * XX, 150 * YY, 70 * XX, 20 * YY);
    
    self.collectBgView.frame = CGRectMake(self.contentView.frame.size.width - 60 * XX, 15 * YY, 40 * XX, 40 * YY);
    self.imageCollect.frame = CGRectMake(self.contentView.frame.size.width - 55 * XX, 21 * YY, 30 * XX, 30 * YY);
}

- (void)collectAction {
    NSDictionary *dict = @{@"collectId" : self.merchantModel.myId?:@"", @"type":@"3"};
    NSString *url = self.imageCollect.isSelected?APP_PLOT_UNCOLLECT:APP_PLOT_COLLECT;
    [GJFNetworkTool POSTRequestWithUrl:url params:dict complete:^(NSDictionary *result) {
        if ([result[@"code"] intValue] == 1000) {
            self.imageCollect.selected = !self.imageCollect.selected;
            self.merchantModel.is_collect = [NSString stringWithFormat:@"%d", ![self.merchantModel.is_collect boolValue]];
        }
        [[[GJFSingleTool topViewController] view] showWarning:result[@"msg"]];
    }];
}

// 收藏按钮背景透明
- (UIView *)collectBgView {
    if (!_collectBgView) {
        _collectBgView = [[UIView alloc] init];
        _collectBgView.backgroundColor = [UIColor blackColor];
        _collectBgView.alpha = 0.5;
        _collectBgView.layer.cornerRadius = 20 * YY;
        [self.contentView addSubview:_collectBgView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collectAction)];
        [_collectBgView addGestureRecognizer:tap];
    }
    return _collectBgView;
}

- (UIButton *)imageCollect {
    if (!_imageCollect) {
        _imageCollect = [UIButton buttonWithType:UIButtonTypeCustom];
        [_imageCollect setImage:[UIImage imageNamed:@"未收藏"] forState:UIControlStateNormal];
        [_imageCollect setImage:[UIImage imageNamed:@"已收藏"] forState:UIControlStateSelected];
        _imageCollect.userInteractionEnabled = NO;
        [self.contentView addSubview:_imageCollect];
    }
    return _imageCollect;
}

- (UIView *)view {
    if (!_view) {
        _view = [[UIView alloc] init];
        _view.alpha = 0.3;
        _view.layer.cornerRadius = 5 * YY;
        _view.clipsToBounds = YES;
        
        self.gradientLayer = [[CAGradientLayer alloc] init];
        self.gradientLayer.colors = [NSArray arrayWithObjects:(id)[UIColor clearColor].CGColor,(id)[UIColor blackColor].CGColor, nil];
        self.gradientLayer.startPoint = CGPointMake(0.5, 0);
        self.gradientLayer.endPoint = CGPointMake(0.5, 1);
        [_view.layer insertSublayer:self.gradientLayer atIndex:0];
        [self.imageV addSubview:_view];
    }
    return _view;
}
- (UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] init];
        _imageV.layer.cornerRadius = 5 * YY;
        _imageV.clipsToBounds = YES;
        _imageV.layer.borderWidth = 2;
        _imageV.layer.borderColor = HOMECOLOR.CGColor;
        _imageV.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView addSubview:_imageV];
    }
    return _imageV;
}
- (UIImageView *)imageUser {
    if (!_imageUser) {
        _imageUser = [[UIImageView alloc] init];
        _imageUser.layer.cornerRadius = 25 * YY;
        _imageUser.clipsToBounds = YES;
        [self.contentView addSubview:_imageUser];
    }
    return _imageUser;
}
- (UILabel *)name {
    if (!_name) {
        _name = [[UILabel alloc] init];
        _name.textColor = [UIColor whiteColor];
        _name.font = [UIFont boldSystemFontOfSize:18 * XX];
        [self.contentView addSubview:_name];
    }
    return _name;
}
- (UILabel *)tag1 {
    if (!_tag1) {
        _tag1 = [[UILabel alloc] init];
        _tag1.textColor = [UIColor whiteColor];
        _tag1.font = [UIFont systemFontOfSize:11 * XX];
        _tag1.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_tag1];
    }
    return _tag1;
}
- (UILabel *)tag2 {
    if (!_tag2) {
        _tag2 = [[UILabel alloc] init];
        _tag2.textColor = [UIColor whiteColor];
        _tag2.font = [UIFont systemFontOfSize:11 * XX];
        _tag2.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_tag2];
    }
    return _tag2;
}
- (UILabel *)tag3 {
    if (!_tag3) {
        _tag3 = [[UILabel alloc] init];
        _tag3.textColor = [UIColor whiteColor];
        _tag3.font = [UIFont systemFontOfSize:11 * XX];
        _tag3.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_tag3];
    }
    return _tag3;
}
- (UIView *)tag1BgView {
    if (!_tag1BgView) {
        _tag1BgView = [[UIView alloc] init];
        _tag1BgView.layer.cornerRadius = 10 * YY;
        _tag1BgView.clipsToBounds = YES;
        _tag1BgView.backgroundColor = [UIColor blackColor];
        _tag1BgView.alpha = 0.5;
        [self.contentView  addSubview:_tag1BgView];
    }
    return _tag1BgView;
}
- (UIView *)tag2BgView {
    if (!_tag2BgView) {
        _tag2BgView = [[UIView alloc] init];
        _tag2BgView.layer.cornerRadius = 10 * YY;
        _tag2BgView.clipsToBounds = YES;
        _tag2BgView.backgroundColor = [UIColor blackColor];
        _tag2BgView.alpha = 0.5;
        [self.contentView  addSubview:_tag2BgView];
    }
    return _tag2BgView;
}
- (UIView *)tag3BgView {
    if (!_tag3BgView) {
        _tag3BgView = [[UIView alloc] init];
        _tag3BgView.layer.cornerRadius = 10 * YY;
        _tag3BgView.clipsToBounds = YES;
        _tag3BgView.backgroundColor = [UIColor blackColor];
        _tag3BgView.alpha = 0.5;
        [self.contentView  addSubview:_tag3BgView];
    }
    return _tag3BgView;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
